package com.mrent.web.controller.front;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.google.appengine.api.datastore.Blob;
import com.mrent.entity.Member;
import com.mrent.entity.Picture;
import com.mrent.service.MemberService;
import com.mrent.service.PictureService;
import com.mrent.util.Constance;
import com.mrent.util.RenderUtil;
import com.mrent.web.form.MemberPhotoForm;

@Controller
public class MemberController {
	
	@Autowired
	private PictureService pictureService;
	
	@Autowired
	private MemberService memberService;
	
	/**
	 * 跳转用户注册.
	 * 
	 * @return
	 */
	@RequestMapping("/member/reg")
	public String reg(){
		
		return "front/member/reg";
	}
	
	/**
	 * 用户注册保存.
	 * 
	 * @return
	 */
	@RequestMapping("/member/save")
	public String save(Member member,MemberPhotoForm photoForm,HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException{
		Member mm = memberService.findByMemberName(member.getMemberName());
		if(mm == null){
			//处理头像图片.
			MultipartFile[] files = photoForm.getFiles();
			for(MultipartFile file :files){
				
				InputStream stream = file.getInputStream();
				Blob productImg = new Blob(IOUtils.toByteArray(stream));
				Picture picture = new Picture();
				picture.setPictureData(productImg);
				pictureService.savePicture(picture);
				member.setMemberPhoto(picture.getPictureId());
				
			}
			member.setMemberPwd(DigestUtils.shaHex(member.getMemberPwd()));
			memberService.saveMember(member);
			
			//会员会话保存.
			session.setAttribute(Constance.SESSION_MEMBER_NAME, member);
			
			Map<String,Object> json = new HashMap<String,Object>();
			json.put("result", "Success");
			String jsonString = JSONObject.fromObject(json).toString();
			RenderUtil.renderHtml(jsonString, response);
		}else{
			Map<String,Object> json = new HashMap<String,Object>();
			json.put("result", "Faile");
			json.put("ErrorCode", "-1");
			String jsonString = JSONObject.fromObject(json).toString();
			RenderUtil.renderHtml(jsonString, response);
		}
		return null;
	}
	
	/**
	 * 会员登录.
	 * 
	 * @param member
	 * @return
	 */
	@RequestMapping("/member/login")
	public String login(Member member,HttpServletResponse response,HttpSession session){
		Member mm = memberService.findByMemberName(member.getMemberName());
		if(mm != null){
			if(DigestUtils.shaHex(member.getMemberPwd()).equalsIgnoreCase(mm.getMemberPwd())){
				session.setAttribute(Constance.SESSION_MEMBER_NAME, mm);
				Map<String,Object> json = new HashMap<String,Object>();
				json.put("result", "Success");
				RenderUtil.renderJson(json, response);
			}else{
				Map<String,Object> json = new HashMap<String,Object>();
				json.put("result", "Faile");
				json.put("ErrorCode", "-2");
				RenderUtil.renderJson(json, response);
			}
		}else{
			Map<String,Object> json = new HashMap<String,Object>();
			json.put("result", "Faile");
			json.put("ErrorCode", "-1");
			RenderUtil.renderJson(json, response);
		}
		return null;
	}
	
	/**
	 * 会员注销.
	 * 
	 * @return
	 */
	@RequestMapping("/member/logout")
	public String logout(HttpSession session){
		session.removeAttribute(Constance.SESSION_MEMBER_NAME);
		return "redirect:/index";
	}
	

}
